$(function () {
    
    var layer = layui.layer
     var form = layui.form
    var laypage = layui.laypage
   /*  var dropdown = layui.dropdown */
    var adSizeList = []
    var form = layui.form
    var layer = layui.layer
    var q = {
        advimgdesc: $('[name=advimgdesc]').val(),
        advimgpos:$('[name=advimgpos]').val(),
        page:'1',
        limit: '',
    }
    initAdvList()
    initAdvPos()

    function initAdvList() {
        console.log('ok');
        $.ajax({
            method: 'GET',
            url: 'http://124.223.14.236:8060/admin/ydd_advimg/list',
            headers : {
                'x-token': "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaW5mbyI6eyJpZCI6IjEiLCJsb2dpbl9uYW1lIjoiYWRtaW4iLCJwYXNzd29yZCI6Ijk2ZTc5MjE4OTY1ZWI3MmM5MmE1NDlkZDVhMzMwMTEyIiwibmFtZSI6IlRoaW5rSlPkuIDlk6UiLCJlbWFpbCI6Imhld3UxMTIyQDE2My5jb20iLCJwaG9uZSI6IjE3MzQyMDY1OTA5IiwibG9naW5faXAiOiI6OjEiLCJsb2dpbl9kYXRlIjoiMjAxOC0wMS0wOCAxNjoyMjo1MSIsImNyZWF0ZV9kYXRlIjoiMjAxNy0xMi0xNCAxMTo1Mjo0NiIsInVwZGF0ZV9kYXRlIjoiMjAyMS0wOS0wOCAxOTo0MzowMyIsImRlbF9mbGFnIjoiMCIsInJvbGVfaWQiOlsiMWM1NGUwMDNjMWZjNGRjZDliMDg3ZWY4ZDQ4YWJhYzMiXSwic3RhdHVzIjoxLCJkZWZhdWx0X2RhdGEiOiIxIiwicm9sZW5hbWUiOlsi6LaF57qn566h55CG5ZGYIl19LCJpYXQiOjE2NTQ3ODY5MzZ9.VFu0p1w6gPzm9t3w5zDqnciWVSbFG6ZIz3ReIC2ylEQ"
            },
            data:q,
            success: function (res) {
                /* if (res.status !== 200) {
                    return console.log('获取失败!');
                } */
                console.log(res);
                var htmlStr = template('tpl_advimg', res)
                $('tbody').html(htmlStr)
                renderPage(res.data.count)
            }
        })
    }

    function renderPage(total) {        
        laypage.render({
            elem: 'page-box' //注意，这里的 test1 是 ID，不用加 # 号
            ,count: total //数据总数，从服务端得到
            , limit: q.page, //设置当前页码位置 防止重新渲染回到起始首页
            curr: q.limit,
            limits:[2,3,4,5],
            layout:	['count','limit','prev', 'page', 'next'],
            jump: function(obj, first){
                //obj包含了当前分页的所有参数，比如：
                console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
                console.log(obj.limit); //得到每页显示的条数
                q.limit = obj.curr 
                q.page = obj.limit
               
                //首次不执行
                if(!first){
                  //do something
                  initAdvList()
                }
              }
        });
    }

    function initAdvPos() {
            $.ajax({
              method: 'GET',
              url: 'http://124.223.14.236:8060/admin/ydd_advpos/all',
              headers : {
                'x-token': "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaW5mbyI6eyJpZCI6IjEiLCJsb2dpbl9uYW1lIjoiYWRtaW4iLCJwYXNzd29yZCI6Ijk2ZTc5MjE4OTY1ZWI3MmM5MmE1NDlkZDVhMzMwMTEyIiwibmFtZSI6IlRoaW5rSlPkuIDlk6UiLCJlbWFpbCI6Imhld3UxMTIyQDE2My5jb20iLCJwaG9uZSI6IjE3MzQyMDY1OTA5IiwibG9naW5faXAiOiI6OjEiLCJsb2dpbl9kYXRlIjoiMjAxOC0wMS0wOCAxNjoyMjo1MSIsImNyZWF0ZV9kYXRlIjoiMjAxNy0xMi0xNCAxMTo1Mjo0NiIsInVwZGF0ZV9kYXRlIjoiMjAyMS0wOS0wOCAxOTo0MzowMyIsImRlbF9mbGFnIjoiMCIsInJvbGVfaWQiOlsiMWM1NGUwMDNjMWZjNGRjZDliMDg3ZWY4ZDQ4YWJhYzMiXSwic3RhdHVzIjoxLCJkZWZhdWx0X2RhdGEiOiIxIiwicm9sZW5hbWUiOlsi6LaF57qn566h55CG5ZGYIl19LCJpYXQiOjE2NTQ3ODY5MzZ9.VFu0p1w6gPzm9t3w5zDqnciWVSbFG6ZIz3ReIC2ylEQ"
              },
              success: function (res) {   
                if(res.data.length > 0){
                    $.each(res.data, function (index, item) {
                      adSizeList = res.data
                    $('#selectList').append(new Option(item.advposname,item.id))
                    })
                    form.render('select')
                }
              }       
            })
          }

$('tbody').on('click', '#btnDel', function () {
    console.log('ok', $(this).attr('data-id'));
    var id = $(this).attr('data-id') //为删除按钮添加自定义属性 并获取自定义属性 拿到数据 
    //二次确认删除弹出框
    layer.confirm('确定要删除吗?', {icon: 3, title:'提示'}, function(index){
        //do something
        $.ajax({
            url: 'http://124.223.14.236:8060/admin/ydd_advimg/del',
            method: 'GET',
            headers : {
                'x-token': "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaW5mbyI6eyJpZCI6IjEiLCJsb2dpbl9uYW1lIjoiYWRtaW4iLCJwYXNzd29yZCI6Ijk2ZTc5MjE4OTY1ZWI3MmM5MmE1NDlkZDVhMzMwMTEyIiwibmFtZSI6IlRoaW5rSlPkuIDlk6UiLCJlbWFpbCI6Imhld3UxMTIyQDE2My5jb20iLCJwaG9uZSI6IjE3MzQyMDY1OTA5IiwibG9naW5faXAiOiI6OjEiLCJsb2dpbl9kYXRlIjoiMjAxOC0wMS0wOCAxNjoyMjo1MSIsImNyZWF0ZV9kYXRlIjoiMjAxNy0xMi0xNCAxMTo1Mjo0NiIsInVwZGF0ZV9kYXRlIjoiMjAyMS0wOS0wOCAxOTo0MzowMyIsImRlbF9mbGFnIjoiMCIsInJvbGVfaWQiOlsiMWM1NGUwMDNjMWZjNGRjZDliMDg3ZWY4ZDQ4YWJhYzMiXSwic3RhdHVzIjoxLCJkZWZhdWx0X2RhdGEiOiIxIiwicm9sZW5hbWUiOlsi6LaF57qn566h55CG5ZGYIl19LCJpYXQiOjE2NTQ3ODY5MzZ9.VFu0p1w6gPzm9t3w5zDqnciWVSbFG6ZIz3ReIC2ylEQ"
            },
            data: {
                id:id
            },
            success: function (res) {
                /* if (res.status !== 0) {
                    return layer.msg(res.errmsg)
                } */
                layer.msg('删除成功!')
                initAdvList()
            }
            
        })
        layer.close(index);
      });
   
})
    var layerindex = null
$('tbody').on('click', '#editBtn', function () {
    //var cate_id = null
    console.log('点击了编辑按钮');
    layerindex = layer.open({
        type: 1,
        area: ['700px', '600px'],
        title: '修改广告图'
        , content: $('#tpl-EditAdv').html()
    });
    var id = $(this).attr('data-id')
    console.log(id);
    $.ajax({
        method: 'GET',
        url: 'http://124.223.14.236:8060/admin/ydd_advimg/rowInfo',
        headers : {
            'x-token': "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaW5mbyI6eyJpZCI6IjEiLCJsb2dpbl9uYW1lIjoiYWRtaW4iLCJwYXNzd29yZCI6Ijk2ZTc5MjE4OTY1ZWI3MmM5MmE1NDlkZDVhMzMwMTEyIiwibmFtZSI6IlRoaW5rSlPkuIDlk6UiLCJlbWFpbCI6Imhld3UxMTIyQDE2My5jb20iLCJwaG9uZSI6IjE3MzQyMDY1OTA5IiwibG9naW5faXAiOiI6OjEiLCJsb2dpbl9kYXRlIjoiMjAxOC0wMS0wOCAxNjoyMjo1MSIsImNyZWF0ZV9kYXRlIjoiMjAxNy0xMi0xNCAxMTo1Mjo0NiIsInVwZGF0ZV9kYXRlIjoiMjAyMS0wOS0wOCAxOTo0MzowMyIsImRlbF9mbGFnIjoiMCIsInJvbGVfaWQiOlsiMWM1NGUwMDNjMWZjNGRjZDliMDg3ZWY4ZDQ4YWJhYzMiXSwic3RhdHVzIjoxLCJkZWZhdWx0X2RhdGEiOiIxIiwicm9sZW5hbWUiOlsi6LaF57qn566h55CG5ZGYIl19LCJpYXQiOjE2NTQ3ODY5MzZ9.VFu0p1w6gPzm9t3w5zDqnciWVSbFG6ZIz3ReIC2ylEQ"
        },
        data: {
            id:id
        },
        success: function (res) {
           /*  if (res.status !== 0) {
                return layer.msg(res.message)
            } */
       //快速给form表单赋值
            
            form.val('formedit', res.data)
            $('#img').attr('src', 'http://124.223.14.236:8060/' + res.data.advimgsrc)
            $('#select select').append(new Option(res.data.advimgpos,res.data.id))
            if(adSizeList.length > 0){
                $.each(adSizeList, function (index, item) {
                  $('#select select').append(new Option(item.advposname,item.id))
                })
               form.render('select');
            }
         
           
        }
    })
    form.on('submit(formedit)', function (data) {
        var formedit = form.val('formedit')
        var sendData = data.field
            var advimgsrc = 'upload/images/20211101/1635756052798.jpg'
            
       // var advimgsrc = 'http://124.223.14.236:8060/upload/images/20211101/1635756052798.jpg'
            sendData.advimgsrc = advimgsrc
            sendData.id = id
        $.ajax({    
            method: 'POST',
            url: 'http://124.223.14.236:8060/admin//ydd_advimg/update',
        
            headers : {
                'x-token': "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaW5mbyI6eyJpZCI6IjEiLCJsb2dpbl9uYW1lIjoiYWRtaW4iLCJwYXNzd29yZCI6Ijk2ZTc5MjE4OTY1ZWI3MmM5MmE1NDlkZDVhMzMwMTEyIiwibmFtZSI6IlRoaW5rSlPkuIDlk6UiLCJlbWFpbCI6Imhld3UxMTIyQDE2My5jb20iLCJwaG9uZSI6IjE3MzQyMDY1OTA5IiwibG9naW5faXAiOiI6OjEiLCJsb2dpbl9kYXRlIjoiMjAxOC0wMS0wOCAxNjoyMjo1MSIsImNyZWF0ZV9kYXRlIjoiMjAxNy0xMi0xNCAxMTo1Mjo0NiIsInVwZGF0ZV9kYXRlIjoiMjAyMS0wOS0wOCAxOTo0MzowMyIsImRlbF9mbGFnIjoiMCIsInJvbGVfaWQiOlsiMWM1NGUwMDNjMWZjNGRjZDliMDg3ZWY4ZDQ4YWJhYzMiXSwic3RhdHVzIjoxLCJkZWZhdWx0X2RhdGEiOiIxIiwicm9sZW5hbWUiOlsi6LaF57qn566h55CG5ZGYIl19LCJpYXQiOjE2NTQ3ODY5MzZ9.VFu0p1w6gPzm9t3w5zDqnciWVSbFG6ZIz3ReIC2ylEQ"
            },
            data: sendData,
            success: function (res) {
               /*  if (res.status !== 200) {
                    return alert('新增失败!')
                } */
                console.log('修改成功!');
                layer.close(layerindex)
                initAdvList()
            }
        })
    })   
         
})
    
//筛选功能

    
    
    
    
  
    $('.add').on('click', function () {
        layerindex = layer.open({
            type: 1,
            area: ['700px', '500px'],
            title: '修改广告图'
            , content: $('#tpl-addAdv').html()
        });

       
        if(adSizeList.length > 0){
            $.each(adSizeList, function (index, item) {
              $('#Addselect').append(new Option(item.advposname,item.id))
            })
            form.render('select');
        }

        
    })

    

    $('body').on('click','#uploadfile', function () {
        // console.log('ok');
      
        $('#file').click()


        
    })

   /*  $("body").on('submit', '#formAdd', function (e) {
        e.preventDefault()
        var data = $(this).serialize()
        var advimgsrc = 'http://124.223.14.236:8060/upload/images/20211101/1635756052798.jpg'
        data.advimgsrc = advimgsrc
        $.ajax({
            method: 'POST',
            url: 'http://124.223.14.236:8060/admin/ydd_advimg/add',
            headers : {
                'x-token': "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaW5mbyI6eyJpZCI6IjEiLCJsb2dpbl9uYW1lIjoiYWRtaW4iLCJwYXNzd29yZCI6Ijk2ZTc5MjE4OTY1ZWI3MmM5MmE1NDlkZDVhMzMwMTEyIiwibmFtZSI6IlRoaW5rSlPkuIDlk6UiLCJlbWFpbCI6Imhld3UxMTIyQDE2My5jb20iLCJwaG9uZSI6IjE3MzQyMDY1OTA5IiwibG9naW5faXAiOiI6OjEiLCJsb2dpbl9kYXRlIjoiMjAxOC0wMS0wOCAxNjoyMjo1MSIsImNyZWF0ZV9kYXRlIjoiMjAxNy0xMi0xNCAxMTo1Mjo0NiIsInVwZGF0ZV9kYXRlIjoiMjAyMS0wOS0wOCAxOTo0MzowMyIsImRlbF9mbGFnIjoiMCIsInJvbGVfaWQiOlsiMWM1NGUwMDNjMWZjNGRjZDliMDg3ZWY4ZDQ4YWJhYzMiXSwic3RhdHVzIjoxLCJkZWZhdWx0X2RhdGEiOiIxIiwicm9sZW5hbWUiOlsi6LaF57qn566h55CG5ZGYIl19LCJpYXQiOjE2NTQ3ODY5MzZ9.VFu0p1w6gPzm9t3w5zDqnciWVSbFG6ZIz3ReIC2ylEQ"
            },
            data: data,
            success: function (res) {
                if (res.status !== 200) {
                    return alert('新增失败!')
                }
                console.log('新增成功!');

            }
        })
    }) */
    
    form.on('submit(formDemo)', function (data) {
        var formadd = form.val('formAdd')
        var sendData = data.field
       
        var advimgsrc = 'upload/images/20211101/1635756052798.jpg'
       // var advimgsrc = 'http://124.223.14.236:8060/upload/images/20211101/1635756052798.jpg'
        sendData.advimgsrc = advimgsrc
        $.ajax({    
            method: 'POST',
            url: 'http://124.223.14.236:8060/admin/ydd_advimg/add',
        
            headers : {
                'x-token': "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaW5mbyI6eyJpZCI6IjEiLCJsb2dpbl9uYW1lIjoiYWRtaW4iLCJwYXNzd29yZCI6Ijk2ZTc5MjE4OTY1ZWI3MmM5MmE1NDlkZDVhMzMwMTEyIiwibmFtZSI6IlRoaW5rSlPkuIDlk6UiLCJlbWFpbCI6Imhld3UxMTIyQDE2My5jb20iLCJwaG9uZSI6IjE3MzQyMDY1OTA5IiwibG9naW5faXAiOiI6OjEiLCJsb2dpbl9kYXRlIjoiMjAxOC0wMS0wOCAxNjoyMjo1MSIsImNyZWF0ZV9kYXRlIjoiMjAxNy0xMi0xNCAxMTo1Mjo0NiIsInVwZGF0ZV9kYXRlIjoiMjAyMS0wOS0wOCAxOTo0MzowMyIsImRlbF9mbGFnIjoiMCIsInJvbGVfaWQiOlsiMWM1NGUwMDNjMWZjNGRjZDliMDg3ZWY4ZDQ4YWJhYzMiXSwic3RhdHVzIjoxLCJkZWZhdWx0X2RhdGEiOiIxIiwicm9sZW5hbWUiOlsi6LaF57qn566h55CG5ZGYIl19LCJpYXQiOjE2NTQ3ODY5MzZ9.VFu0p1w6gPzm9t3w5zDqnciWVSbFG6ZIz3ReIC2ylEQ"
            },
            data: sendData,
            success: function (res) {
               /*  if (res.status !== 200) {
                    return alert('新增失败!')
                } */
                console.log('新增成功!');
                layer.close(layerindex)
                initAdvList()
            }
        })
    })
})

